/*
 * For com.royal.art
 * Copyright [2015/11/14] By FCHEN
 */
package com.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.beans.Dept;
import com.beans.SysRole;
import com.beans.SysUser;
import com.beans.page.PageList;
import com.data.mapper.BaseMapper;
import com.data.mapper.DeptMapper;
import com.data.mapper.SysRoleMapper;
import com.data.mapper.SysUserMapper;
import com.services.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * FootballSysUserServiceImpl
 * 系统用户
 *
 * @author FCHEN
 * @date 2015/11/14
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public BaseMapper<SysUser> getMapper() {
        return sysUserMapper;
    }

    /**
     * 根据用户账号查询
     *
     * @param account
     * @return
     */
    public SysUser queryUserByAccount(String account) {
        return sysUserMapper.queryUserByAccount(account);
    }

    /**
     * 更新用户登录次数
     *
     * @param sysUser
     * @return
     */
    public int updateLoginTimes(SysUser sysUser) {
        return sysUserMapper.updateLoginTimes(sysUser);
    }

    /**
     * 根据用户账户修改用户密码
     *
     * @param param
     * @return
     */
    public int updateUserPwd(HashMap<String, String> param) {
        return sysUserMapper.updateUserPwd(param);
    }

    /***
     * 更新用户菜单的关系(权限)
     * @param sysUser
     */
    public void deleteUserOfMenu(SysUser sysUser){
        sysUserMapper.deleteUserOfMenu(sysUser);
    }
    public void addUserOfMenu(SysUser sysUser){
        sysUserMapper.addUserOfMenu(sysUser);
    }

    public PageList<SysUser> queryPageByUser(PageList<SysUser> pageList, SysUser bean) {
        Page<SysUser> page;
        int pageStart;
        if (pageList.getiDisplayLength() == -1) {//不分页
            pageStart = 1;
            page = PageHelper.startPage(pageStart, 10000, false);
        } else {
            pageStart = pageList.getiDisplayStart() / pageList.getiDisplayLength() + 1;
            page = PageHelper.startPage(pageStart, pageList.getiDisplayLength());
        }
        getMapper().queryList(bean);
        pageList.setiTotalRecords(page.getTotal());
        pageList.setiTotalDisplayRecords(pageList.getiTotalRecords());

        List<SysUser> users = new ArrayList<>();
        if(page.size()>0){
            for (SysUser user : page){
                SysRole role = sysRoleMapper.queryById(Long.valueOf(user.getUserRole()));
                Dept dept = deptMapper.queryById(user.getDeptId());
                user.setrName(role.getRoleName());
                user.setdName(dept.getDeptName());

                users.add(user);
            }
        }
        pageList.setAaData(users);
        return pageList;
    }

    public int modifyUser(SysUser sysUser){
        return sysUserMapper.modifyUser(sysUser);
    }

    @Override
    public SysUser queryUserOnlyById(Long id) {
        return sysUserMapper.queryUserOnlyById(id);
    }
}
